package com.jinghua.treatment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jinghua.treatment.entity.*;
import com.jinghua.treatment.entity.vo.DoctorVO;
import com.jinghua.treatment.entity.vo.PatientVO;
import com.jinghua.treatment.mapper.*;
import com.jinghua.treatment.service.SysPatientService;
import com.jinghua.treatment.tools.DateUtil;
import com.jinghua.treatment.tools.IDGenerator;
import com.jinghua.treatment.wechat.util.HttpUtil;
import com.jinghua.treatment.wechat.util.WechatUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 患者业务接口实现类
 * Created by 王兴光 on 2018/3/16.
 */
@Service("patientService")
public class SysPatientServiceImpl implements SysPatientService {

    @Resource
    private SysPatientMapper patientMapper;
    @Resource
    private SysDoctorMapper sysDoctorMapper;
    @Resource
    private SysWechatAccountMapper sysWechatAccountMapper;
    @Resource
    private DPRelationshipMapper dpRelationshipMapper;
    @Resource
    private SysNoticeMapper sysNoticeMapper;



    @Override
    public List<SysPatient> selectByDoctorId(Integer doctorId) {
        return patientMapper.selectBydoctorId(doctorId);
    }

    @Override
    public List<SysPatient> selectByWeek(Integer doctorId) {
        return patientMapper.selectByTime(doctorId);
    }

    @Override
    public List<SysPatient> selectByName(Integer doctorId, String patientName) {
        return patientMapper.selectByName(doctorId,patientName);
    }

    @Override
    @Transactional
    public int insert(SysPatient record) {
        return patientMapper.insert(record);
    }

    @Override
    @Transactional
    public int updateByPrimaryKey(SysPatient record) {
        return patientMapper.updateByPrimaryKey(record);
    }


    public void updateByVO(PatientVO record) {
        if(record.getBirthdayStr()!=null)
            record.setBirthday(DateUtil.getDateFromStr(record.getBirthdayStr(),"yyyy年MM月dd日"));
        patientMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void update(SysPatient record) {
        patientMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int selectPatientCount(Integer doctorId) {
        return patientMapper.selectPatientCount(doctorId);
    }

    @Override
    public SysPatient selectById(Integer patientId) {
        SysPatient patient = patientMapper.selectByPrimaryKey(patientId);
        if(sysWechatAccountMapper.selectByPatient(patientId) != null) {
            patient.setNickName(sysWechatAccountMapper.selectByPatient(patientId).getNickname());
        }
        return patient;
    }

    @Override
    public SysPatient selectByWechat(String wechat) {
        return patientMapper.selectByWechat(wechat);
    }

    @Override
    public SysPatient selectByPhone(String phone) {
        return patientMapper.selectByPhone(phone);
    }

    @Override
    public SysPatient patientRegiste(String phone, String name, String wid) {
        SysPatient patient  =  new SysPatient();
        patient.setPatientname(name);
        patient.setPatientCreatedate(new Date());
        patient.setPatientModifydate(new Date());
        patient.setPhone(phone);

        // 插入患者数据后，再插入一条微信账号数据
        String accessToken = WechatUtil.getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + wid + "&lang=zh_CN";
        String responseJson = HttpUtil.sendGetRequest(url, null);
        SysWechatAccount wechatAccount = JSONObject.parseObject(responseJson, SysWechatAccount.class);
        wechatAccount.setUuid(IDGenerator.uuid());
        patient.setWechat(wechatAccount.getOpenid());
        patient.setSex("0".equals(wechatAccount.getSex())?"男":"女");

        patientMapper.insert(patient);
        sysWechatAccountMapper.insert(wechatAccount);
        return patient;
    }

    @Override
    public List<DoctorVO> loadMyDoctor(Integer patientId) {
        List<DoctorVO> result = new ArrayList<>();
        DPRelationship dpRelationshipExample  = new DPRelationship();
        dpRelationshipExample.setPatientId(patientId);
        List<DPRelationship> dpRelationships =  dpRelationshipMapper.selectByExample(dpRelationshipExample);
        for(DPRelationship dpRelationship : dpRelationships){
            SysDoctor sysDoctor  = sysDoctorMapper.selectByPrimaryKey( dpRelationship.getDoctorId());
            DoctorVO  doctorVO  = new DoctorVO();
            BeanUtils.copyProperties(sysDoctor,doctorVO);
            //查询 公告和擅长
            SysNotice sysNotice  = sysNoticeMapper.selectByDoctorId(sysDoctor.getDoctorId());
            if(sysNotice!=null){
                doctorVO.setNoticeContent(sysNotice.getContent());
                doctorVO.setSpecialty(sysNotice.getSpecialty());
            }
            result.add(doctorVO);
        }
        return result;
    }
}
